//
//  FansMyViewController.m
//  LiveStar
//
//  Created by 漪珊 on 2016/12/9.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "FansMyViewController.h"
#import "MessageViewController.h"
#import "FansFocusCell.h"
#import "FansMyTopView.h"
#import "BuyViewController.h"
#import "LogoutCell.h"
#import "FansRedPaperViewController.h"
#import "FansRepresentViewController.h"
#import "FansFocusViewController.h"
#import "FansCooperationViewController.h"
#import "LoginViewController.h"
#import "FansInfoViewController.h"
#import "MineLevelViewController.h"


@interface FansMyViewController ()<UITableViewDelegate,UITableViewDataSource,StarTopViewDelegate,LogoutCellDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation FansMyViewController
static NSString *logoutCell = @"logoutCell";


#pragma mark --  退出登录发送请求
- (void)LogoutRequest {
    NSDictionary *dict = @{
                           @"udid":User_ID,
                           @"token":TOKEN
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kAccount_Logout];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        [LSFileCacheManager removeObjectByFileName:kUserPath];
        [LSFileCacheManager removeObjectByFileName:kLOG_TOKEN];
        [self pushToLog];

    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNav];
    
    [self setUpUI];
}
- (void)setNav {
    self.navigationItem.title = @"我";
}


//消息中心
- (void)MessageCenterClick:(UIButton *)sender {
    MessageViewController *messageVc = [MessageViewController new];
    messageVc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:messageVc animated:YES];
}

#pragma mark -- UI
- (void)setUpUI {
    
    //tableView
    [self creatTableView];
    
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.bounces = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"F5F5F5"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    FansMyTopView *topView = [[NSBundle mainBundle] loadNibNamed:@"FansMyTopView" owner:self.tableView options:nil].lastObject;
    topView.delegate = self;
    topView.userInteractionEnabled = YES;
    topView.frame = CGRectMake(0, 0, kSCREEN_WIDTH, 180);
    topView.backgroundColor = [UIColor whiteColor];
    [topView.StarPhoto sd_setImageWithURL:[NSURL URLWithString:[LSUserInfoManager sharedManager].currentUserInfo.u_avatar] placeholderImage:kPlACE_ICON];
    topView.StarPhoto.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(PersonalInfoAction:)];
    [topView.StarPhoto addGestureRecognizer:tap];
    topView.starName.text = [LSUserInfoManager sharedManager].currentUserInfo.u_nickname;
    self.tableView.tableHeaderView = topView;
    [self.view addSubview:self.tableView];
}

#pragma mark -- tableView 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 4) {
        LogoutCell *cell = [tableView dequeueReusableCellWithIdentifier:logoutCell];
        if (!cell) {
            cell = [[LogoutCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:logoutCell];
        }
        cell.delegate = self;
        return cell;
    } else {
        FansFocusCell *cell = [FansFocusCell loadNibCellWithTabelView:tableView];
        if (indexPath.row == 0) {
            cell.itemtitle.text = @"我的等级";
            cell.detail.text = @"";
        } else if (indexPath.row == 1) {
            cell.itemtitle.text = @"我关注的";
            cell.detail.text = @"";
        } else if (indexPath.row == 2) {
            cell.itemtitle.text = @"收到的红包";
            cell.detail.text = @"";
        }else if (indexPath.row == 3) {
            cell.itemtitle.text = @"收到的礼品";
            cell.detail.text = @"";
            cell.sepline.hidden = YES;
        }
        return cell;

    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        MineLevelViewController *levelVC = [[MineLevelViewController alloc] init];
        [self.navigationController pushViewController:levelVC animated:YES];
    } else if (indexPath.row == 1) {
        FansFocusViewController *focusVc = [[FansFocusViewController alloc] init];
        [self.navigationController pushViewController:focusVc animated:YES];
    } else if (indexPath.row == 2) {
        FansRedPaperViewController *fansPaperVc = [[FansRedPaperViewController alloc] init];
        [self.navigationController pushViewController:fansPaperVc animated:YES];
    } else if (indexPath.row == 3) {
        FansRepresentViewController *fansPresentVc = [[FansRepresentViewController alloc] init];
        [self.navigationController pushViewController:fansPresentVc animated:YES];
    }
}

#pragma mark --退出登录
- (void)logout {
    UIAlertController *alertVc = [UIAlertController  alertControllerWithTitle:@"温馨提示" message:@"您确认退出登录吗?" preferredStyle:UIAlertControllerStyleAlert] ;
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self LogoutRequest];
    }];
    [alertVc addAction:action1];
    [alertVc addAction:action2];
    [self presentViewController:alertVc animated:YES completion:nil];
}

- (void)pushToLog {
    QZHNavgationController *nav = [[QZHNavgationController alloc] initWithRootViewController:[LoginViewController new]];
    [UIApplication sharedApplication].keyWindow.rootViewController = nav;
}

#pragma mark -- 区头视图

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 4) {
        return 77;
    }
    return 60;
}

#pragma mark -- FansMyTopViewDelegate(充值)
- (void)Recharge {
    BuyViewController *buyVc = [[BuyViewController alloc] init];
    [self.navigationController pushViewController:buyVc animated:YES];
}

#pragma mark -- 个人中心
- (void)PersonalInfoAction:(UITapGestureRecognizer *)tap {
    FansInfoViewController *infoVC = [[FansInfoViewController alloc] init];
    [self.navigationController pushViewController:infoVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
